package cn.green.mapper.model.impls.field;

import cn.green.core.annos.column.QColumn;
import cn.green.core.components.field.FieldTypeCom;
import cn.green.core.components.store.field.FieldFormTypeStore;
import cn.green.core.components.store.field.FieldTypeStore;
import cn.green.core.model.QFieldModel;
import cn.green.mapper.model.FieldColumnMapper;
import cn.green.mvc.entity.OrmTableEntity;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.lang.reflect.Field;
import java.util.Date;

/**
 * @author wang.yishu
 * @version 1.0
 * @className NormalFieldColumnMapper
 * @description 普通类型（八大基本数据类型+String）的映射
 * @date 2025/7/23 10:19
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class NormalFieldColumnMapper extends FieldColumnMapper {

    private QColumn qColumn;

    @Override
    public void setFieldValue(JSONObject row, OrmTableEntity entity) throws Exception {
        QFieldModel column = this.getColumn();
        String fieldName = column.getFieldName();
        if (row.containsKey(fieldName)) {
            Object val = row.get(fieldName);
            Field field = this.getField();
            if (field != null) {
                field.setAccessible(true);
                Class<?> type = field.getType();
                if (type == String.class) {
                    field.set(entity, String.valueOf(val));
                } else if (type == Integer.class || type == int.class) {
                    field.set(entity, Integer.valueOf(String.valueOf(val)));
                } else if (type == Long.class || type == long.class) {
                    field.set(entity, Long.valueOf(String.valueOf(val)));
                } else if (type == Double.class || type == double.class) {
                    field.set(entity, Double.valueOf(String.valueOf(val)));
                } else if (type == Float.class || type == float.class) {
                    field.set(entity, Float.valueOf(String.valueOf(val)));
                } else if (type == Boolean.class || type == boolean.class) {
                    field.set(entity, Boolean.valueOf(String.valueOf(val)));
                } else if (type == Byte.class || type == byte.class) {
                    field.set(entity, Byte.valueOf(String.valueOf(val)));
                } else if (type == Short.class || type == short.class) {
                    field.set(entity, Short.valueOf(String.valueOf(val)));
                } else if (type == Character.class || type == char.class) {
                    field.set(entity, String.valueOf(val).charAt(0));
                } else if (type == Date.class) {
                    String fieldForm = this.column.getFieldForm();
                    if (fieldForm != null) {
                        Object format = FieldFormTypeStore.get(fieldForm).parse(val);
                        if (format != null) {
                            field.set(entity, format);
                        }
                    }
                } else if (type == java.sql.Date.class) {
                    String fieldForm = this.column.getFieldForm();
                    if (fieldForm != null) {
                        Object format = FieldFormTypeStore.get(fieldForm).parse(val);
                        if (format != null) {
                            field.set(entity,  new java.sql.Date(((Date) format).getTime()));
                        }
                    }
                }
            }
            if (qColumn != null) {
                String fieldType = this.column.getFieldType();
                String viewFieldName = qColumn.viewFieldName();
                Object viewFieldValue;
                if (fieldType != null) {
                    FieldTypeCom fieldTypeCom = FieldTypeStore.get(fieldType);
                    if (fieldTypeCom != null) {
                        viewFieldValue = fieldTypeCom.getViewFieldValue(row, this.column);
                        if (viewFieldValue != null) {
                            if (viewFieldName != null && !viewFieldName.isEmpty()) {
                                try {
                                    Field declaredField = entity.getClass().getDeclaredField(viewFieldName);
                                    declaredField.setAccessible(true);
                                    declaredField.set(entity, viewFieldValue);
                                } catch (Exception e) {
                                    System.err.println("QOrm Field Column ["
                                            + this.column.getSourceName() + "." + this.column.getSchemeName() + "."
                                            + this.column.getTableName() + "." + this.column.getFieldName()
                                            + "] Mapper Error: " + e.getMessage());
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public void setColumnValue(JSONObject row, OrmTableEntity entity) throws Exception {
        Field field = this.getField();
        if (field != null) {
            field.setAccessible(true);
            Object val = field.get(entity);
            QFieldModel column = this.getColumn();
            if (column != null) {
                row.put(column.getFieldName(), val);
            }
        }
    }

//    public Date parseDate(String date) {
//
//    }
//
//    public java.sql.Date parseSqlDate(String date) {
//
//    }
}
